#include "mywindow.h"
#include <QMenuBar>
#include <QStatusBar>
#include <QMessageBox>
#include <QScrollArea>
#include <QPushButton>
#include <QVBoxLayout>
#include <QPainter>

#include <QDebug>
#include <QResizeEvent>

MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent)
{
    this->resize(320*3, 240*3);

    createMenus();
    createToolBars();
    this->statusBar()->showMessage("Welcome");

    newclient = new AddControl(this);

    QScrollArea* scrollArea = new QScrollArea(this);
    scrollArea->setBackgroundRole(QPalette::Shadow);
    scrollArea->setAutoFillBackground(true);
    scrollArea->setWidget(newclient);
    scrollArea->setWidgetResizable(true);

    this->setCentralWidget(scrollArea);
}

void MyWindow::createMenus()
{
    //File Menu
    fileMenu    = menuBar()->addMenu("&File");
    exitAct     = fileMenu->addAction("E&xit");
    newAct      = fileMenu->addAction("N&ew");
    exitAct->setShortcut(QKeySequence("Ctrl+Q"));
    newAct->setShortcut(QKeySequence("Ctrl+N"));

    //Edit Menu
    editMenu    = menuBar()->addMenu("&Edit");
    optionAct   = editMenu->addAction("&Options");

    //Help Menu
    helpMenu    = menuBar()->addMenu("&Help");
    aboutAct    = helpMenu->addAction("&About Me");
    aboutQtAct  = helpMenu->addAction("About &Qt");

    connect(exitAct,SIGNAL(triggered()), this,SLOT(close()));
    connect(newAct, SIGNAL(triggered()), this, SLOT(createclient()));
    connect(optionAct, SIGNAL(triggered()), this, SLOT(myoptions()));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(aboutme()));
    connect(aboutQtAct,SIGNAL(triggered()), this,SLOT(aboutQtActTriggered()));
}

void MyWindow::createToolBars()
{
    mainToolBar = addToolBar("Main");
    mainToolBar->setMovable(true);
    mainToolBar->setIconSize(QSize(32,32));
    //设置图片和文字同时显示
    mainToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    new_w = mainToolBar->addAction("New");
    new_w->setEnabled(true);
    new_w->setIcon(QIcon(":/new.jpeg"));

    shutdown = mainToolBar->addAction("Close");
    shutdown->setEnabled(true);
    shutdown->setIcon(QIcon(":/stop1.png"));

    myfra = new QFrame(this);
    myfra->setAutoFillBackground(true);
    myfra->setEnabled(false);
    myfra->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    img.load(":/head.png");
    mainToolBar->addWidget(myfra);

    connect(new_w, SIGNAL(triggered()), this, SLOT(createclient()));
    connect(shutdown, SIGNAL(triggered()), this, SLOT(close()));
}

void MyWindow::myoptions()
{
    QMessageBox::about(this, "option clicked", "Option Undefined!");
}

void MyWindow::aboutme()
{
    QMessageBox::about(this, "About Me", "Name  : Linux Web Camera\n"\
                                         "Author: Wang Yu\n"\
                                         "Tel   : 18612346666\n"\
                                         "Email : yuwang@yahoo.cn\n");
}

void MyWindow::aboutQtActTriggered()
{
    QMessageBox::aboutQt(this,"About Qt");
}

void MyWindow::createclient()
{
    newclient->publicAddthread();
}

void MyWindow::paintEvent(QPaintEvent *){
    QPainter painter(this);

    QRect source(0, 0, img.width(), img.height());
    QRect target(128, 0, 810, 64);
    painter.drawImage(target, img, source);
}


void MyWindow::closeEvent(QCloseEvent *)
{
    for(int i = 0; i < newclient->imgWidgets.size(); i++){
        newclient->imgWidgets[i]->stopbtnClicked();
    }
    QThread::sleep(1);
    // sleep(1);

}

MyWindow::~MyWindow(){}
